package com.societyGames.flashForms
{
	import com.societyGames.system.EventObject;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class ThreeStateButton extends Sprite
	{
		protected var _buttonClickEventObject:EventObject; //Event
		protected var _up:Sprite;
		protected var _over:Sprite;
		protected var _down:Sprite;
		protected var _current:Sprite;
		
		public function ThreeStateButton(up:Sprite, over:Sprite = null, down:Sprite = null)
		{
			super();
			
			this._buttonClickEventObject = new EventObject(this, "BUTTON_CLICK");
			
			this._up = up;
			if (over == null)
			{
				over = up;
			}
			this._over = over;
			if (down == null)
			{
				down = over;
			}
			this._down = down;
			setSprite(this._up);
			
			this.buttonMode = true;
			this.useHandCursor = true;
			this.mouseChildren = false;
			
			this.addEventListener(MouseEvent.CLICK, internalClickHandler); //must be this.clickHandler to prevent argument collision.
			this.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			this.addEventListener(MouseEvent.MOUSE_UP, upHandler);
		}
		
		public function get buttonClickEventObject():EventObject //Event
		{
			return this._buttonClickEventObject;
		}
		
		protected function internalClickHandler(event:MouseEvent):void
		{
			this._buttonClickEventObject.dispatchEvent();
		}
		
		private function overHandler(event:MouseEvent):void
		{
			setSprite(this._over);
		}
		
		private function outHandler(event:MouseEvent):void
		{
			setSprite(this._up);
		}
		
		private function downHandler(event:MouseEvent):void
		{
			setSprite(this._down);
		}
		
		private function upHandler(event:MouseEvent):void
		{
			setSprite(this._over);
		}
		
		protected function setSprite(sprite:Sprite):void
		{
			if (sprite != this._current)
			{
				if (this._current != null)
				{
					this.removeChild(this._current);
				}
				this._current = sprite;
				this.addChildAt(this._current, 0);
			}
		}
		
		public override function set height(value:Number):void
		{
			this._up.height = value;
			this._over.height = value;
			this._down.height = value;
		}
		
		public override function set width(value:Number):void
		{
			this._up.width = value;
			this._over.width = value;
			this._down.width = value;
		}
	}
}